package com.zs.leymovie.activity;

import android.os.Bundle;
import android.os.Handler.Callback;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.mob.tools.utils.UIHandler;
import com.zs.leymovie.R;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener, Callback, PlatformActionListener {


    // 填写从短信SDK应用后台注册得到的APPKEY
    private static String APPKEY = "27fe7909f8e8";
    // 填写从短信SDK应用后台注册得到的APPSECRET
    private static String APPSECRET = "3c5264e7e05b8860a9b98b34506cfa6e";

    private static final int MSG_USERID_FOUND = 1;
    private static final int MSG_LOGIN = 2;
    private static final int MSG_AUTH_CANCEL = 3;
    private static final int MSG_AUTH_ERROR = 4;
    private static final int MSG_AUTH_COMPLETE = 5;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ShareSDK.initSDK(this);
        setContentView(R.layout.tpl_login_page);

        (findViewById(R.id.tvMsgRegister)).setOnClickListener(this);
        (findViewById(R.id.tvWeixin)).setOnClickListener(this);
        (findViewById(R.id.tvWeibo)).setOnClickListener(this);
        (findViewById(R.id.tvQq)).setOnClickListener(this);


        //SMSSDK.initSDK(this, APPKEY, APPSECRET);
        // 显示提示
        //showDemo();
        //finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvMsgRegister: {
                //短信登录
                //popupMsgLogin();
                //Toast.makeText(this, "未完成短信登录", Toast.LENGTH_SHORT).show();
            }
            break;
            case R.id.tvWeixin: {
                //微信登录
                //测试时，需要打包签名；sample测试时，用项目里面的demokey.keystore
                //打包签名apk,然后才能产生微信的登录
                Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
                authorize(wechat);
            }
            break;
            case R.id.tvWeibo: {
                //新浪微博
                Platform sina = ShareSDK.getPlatform(SinaWeibo.NAME);
                authorize(sina);
            }
            break;
            case R.id.tvQq: {
                //QQ空间
                Platform qzone = ShareSDK.getPlatform(QZone.NAME);
                authorize(qzone);
            }
            break;
            /*case R.id.tvOther: {
                //其他登录
				authorize(null);
			} break;
			case R.id.tvFacebook: {
				//facebook登录
				Dialog dlg = (Dialog) v.getTag();
				dlg.dismiss();
				Platform facebook = ShareSDK.getPlatform(Facebook.NAME);
				authorize(facebook);
			} break;
			case R.id.tvTwitter: {
				//twitter 登录
				Dialog dlg = (Dialog) v.getTag();
				dlg.dismiss();
				Platform twitter = ShareSDK.getPlatform(Twitter.NAME);
				authorize(twitter);
			} break;*/
        }
    }


    private void authorize(Platform plat) {
        if (plat.isValid()) {
            String userId = plat.getDb().getUserId();
            if (!TextUtils.isEmpty(userId)) {
                UIHandler.sendEmptyMessage(MSG_USERID_FOUND, this);
                login(plat.getName(), userId, null);
                return;
            }
        }

        //关闭SSO授权，用网页授权代替；true-关闭；flase，开启
        plat.SSOSetting(true);

        //设置监听
        plat.setPlatformActionListener(this);


        //执行授权
        // plat.authorize();
        plat.showUser(null);
    }

    @Override
    @SuppressWarnings("unchecked")
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_USERID_FOUND: {
                Toast.makeText(this, R.string.userid_found, Toast.LENGTH_SHORT).show();
            }
            break;
            case MSG_LOGIN: {
                String text = getString(R.string.logining, msg.obj);
                Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
                finish();
                System.out.println("---------------");

//				Builder builder = new Builder(this);
//				builder.setTitle(R.string.if_register_needed);
//				builder.setMessage(R.string.after_auth);
//				builder.setPositiveButton(R.string.ok, null);
//				builder.create().show();
            }
            break;
            case MSG_AUTH_CANCEL: {
                Toast.makeText(this, R.string.auth_cancel, Toast.LENGTH_SHORT).show();
                System.out.println("-------MSG_AUTH_CANCEL--------");
            }
            break;
            case MSG_AUTH_ERROR: {
                Toast.makeText(this, R.string.auth_error, Toast.LENGTH_SHORT).show();
                System.out.println("-------MSG_AUTH_ERROR--------");
            }
            break;
            case MSG_AUTH_COMPLETE: {
                Toast.makeText(this, R.string.auth_complete, Toast.LENGTH_SHORT).show();
                System.out.println("--------MSG_AUTH_COMPLETE-------");
            }
            break;
        }
        return false;
    }

    //成功，在这里实现跳转，根据业务需求跳转
    @Override
    public void onComplete(Platform platform, int action, HashMap<String, Object> hashMap) {
        if (action == Platform.ACTION_USER_INFOR) {
            UIHandler.sendEmptyMessage(MSG_AUTH_COMPLETE, this);
            login(platform.getName(), platform.getDb().getUserId(), hashMap);
        }
        //System.out.println(hashMap);
        Toast.makeText(this, "------User Name ---------" + platform.getDb().getUserName(), Toast.LENGTH_LONG).show();
        System.out.println("------User ID ---------" + platform.getDb().getUserId());
        Toast.makeText(this, platform.getDb().getUserName() + platform.getDb().getUserId() + "最后终于轮到我了", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onError(Platform platform, int action, Throwable throwable) {
        if (action == Platform.ACTION_USER_INFOR) {
            UIHandler.sendEmptyMessage(MSG_AUTH_ERROR, this);
        }
        //清楚已授权的信息
        platform.removeAccount();
        throwable.printStackTrace();
    }

    @Override
    public void onCancel(Platform platform, int action) {
        if (action == Platform.ACTION_USER_INFOR) {
            UIHandler.sendEmptyMessage(MSG_AUTH_CANCEL, this);
        }
    }

    private void login(String plat, String userId, HashMap<String, Object> userInfo) {
        Message msg = new Message();
        msg.what = MSG_LOGIN;
        msg.obj = plat;
        Toast.makeText(this, plat, Toast.LENGTH_SHORT).show();
        UIHandler.sendMessage(msg, this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ShareSDK.stopSDK();
    }
}